<?php

namespace app\model;

use app\stateData;
use think\Model;

class UnitInfo extends Model
{
    /**
     * 单位资料库
     */
    protected $createTime = 'create_date';

    public function getRegisterPriceAttr($name)
    {
        if (empty($name)) return "";
        return $name;
    }

    public function getSecureExpireDateAttr($name)
    {
        if (empty($name) || $name == "0000-00-00 00:00:00") return "";
        return date ('Y-m-d',strtotime ($name));
    }
    public function getEstablishDateAttr($name)
    {
        if (empty($name) || $name == "0000-00-00 00:00:00") return "";
        return date ('Y-m-d',strtotime ($name));
    }
    public function getAreaAttr($name,$data)
    {
        if (empty($name) || $name == 0) return "";
        return (new SystemChina)->getAreaDataModel ($data['province'],$data['city'],$name)?:'';
    }

    public function addDataModel($data)
    {
        //先判断是否存在本条title数据
        $find = $this->where(['title'=>$data['title']])->findOrEmpty ();
        if (!$find->isEmpty ()){
            return true;
        }else{ //为空添加
            return self::create($data);
        }
    }

    public function getTypeListModel($type, $page, $pageNum, $ids)
    {
        return $this->where(['type' => $type])->where('id', 'in', $ids)->select()->toArray ();
    }
    public function getTypeListCountModel($type): int
    {
        return $this->where(['type' => $type])->count();
    }

    public function newGetTypeListCountModel($type, $ids)
    {
        return $this->where(['type' => $type])->where('id', 'in', $ids)->select();
    }

    public function editTypeDataModel($data)
    {
        return self::update($data);
    }

    public function delIdModel($id)
    {
        return self::destroy($id);
    }

    public function getLikeName($name)
    {
        return $this->whereLike('title', '%' . $name . '%')->field('title,id')->select()->toArray();
    }

    public function getTypeDataModel($type,$id)
    {
        return $this->where(['type' => $type])->whereIn('id',$id)->select()->toArray();
    }

    /**
     * 获取单条信息
     * @param $id
     * @return array|null
     */
    public function getIdDataModel($id)
    {
        $data = $this->where(['id'=>$id])->findOrEmpty();
        if (!$data->isEmpty()){
            return $data->toArray();
        }
        return null;
    }
}